<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Edit</title>
    </h:head>
    <h:body>
        <ui:composition template="../../templates/master.xhtml">

            <ui:define name="content">

                <p:panel id="panel" header="Edit category">
                    <p:messages id="messages" />
                    <h:panelGrid columns="3">
                        <h:outputText value="Name"/>
                        <p:inputText id="name" value="#{categoriesController.selected.name}" required="true">
                            <f:validateLength for="name" maximum="255"/>
                            <f:validateRequired for="name" />
                        </p:inputText>
                        <p:message for="name"/>

                        <h:outputText   value="Description" />
                        <p:inputTextarea id="description" value="#{categoriesController.selected.description}"/>
                        <h:message for="description"/>

                        <h:outputText value="Cat icon"/>
                        <h:form enctype="multipart/form-data"> 
                            <img id="currentCategoryImage" src="#{categoriesController.selected.icon}"/>
                            <p:fileUpload id="categoryIcon"   
                                          mode="advanced"   
                                          auto="true"
                                          fileUploadListener="#{categoriesController.handleFileUpload}"
                                          update="categoryIconMessage" 
                                          sizeLimit="100000"   
                                          allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>  
                            <p:growl id="categoryIconMessage" showDetail="true"/>  
                        </h:form>
                        <p:graphicImage id="imgCatIcon" style="display:none"/>
                    </h:panelGrid>
                    <f:facet name="footer">
                        <table width="100%">
                            <tr>
                                <td width="30%">
                                    <p:commandButton update="panel, messages" value="Save" action="#{categoriesController.update()}"/>
                                </td>
                                <td align="right"> 
                                    <p:commandButton ajax="false" immediate="true" action="#{categoriesController.prepareList()}" value="List"/> 
                                    <p:commandButton ajax="false" immediate="true" action="#{categoriesController.prepareView()}" value="View Detail"/>
                                </td>
                            </tr>
                        </table>

                    </f:facet>
                </p:panel>

                <script type="text/javascript">
                    $(document).ready(function(){
                       $("#form\\:j_idt46\\:categoryIcon_input").live("done",function(e,data){
                           console.log(e);
                           console.log(data);
                       });
                       $("#form\\:j_idt46\\:categoryIcon_input").live('fileuploadsubmit',function(e,data){
                            console.log(e);
                       });
                    });
                </script>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

